package com.sat.wisdom.Views.Impl;

import android.app.Activity;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.sat.wisdom.Activities.MainActivity;
import com.sat.wisdom.Domain.NewsCategory;
import com.sat.wisdom.Fragments.LeftMenuFragment;
import com.sat.wisdom.Utils.CacheUtil;
import com.sat.wisdom.Utils.Constant;
import com.sat.wisdom.Views.Bases.BaseMenuDetailPage;
import com.sat.wisdom.Views.Bases.BasePage;
import com.sat.wisdom.Views.Impl.LeftMenu.InteractMenuDetailsPager;
import com.sat.wisdom.Views.Impl.LeftMenu.NewsMenuDetailsPager;
import com.sat.wisdom.Views.Impl.LeftMenu.PictureMenuDetailsPager;
import com.sat.wisdom.Views.Impl.LeftMenu.TopicMenuDetailsPager;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;

/**
 * Project:Wisdom
 * Package:com.sat.wisdom.Views.Impl
 * Created by 透野君爱打奶炮 on 2018-04-25 at 19:49
 */
public class NewsCenterPage extends BasePage {
    private ArrayList<BaseMenuDetailPage> left_menu_pages;
    private NewsCategory newsCategory;

    public NewsCenterPage(Activity activity) {
        super(activity);
    }

    @Override
    public View initView() {
        return super.initView();
    }

    @Override
    public void initData() {
        Log.d("TAG","News页加载中...");
//        TextView textView = new TextView(mActivity);
//        textView.setText("新闻中心");
//        textView.setTextColor(Color.RED);
//        textView.setTextSize(22);
//        textView.setGravity(Gravity.CENTER);
//        fl_page_content.removeAllViews();
//        fl_page_content.addView(textView);

        //判断本地是否有缓存数据
        String cache = CacheUtil.getCache(mActivity, Constant.SERVER_CATEGORY_URL);
        if (!TextUtils.isEmpty(cache)){
            //非空，不请求服务器
            Log.d("TAG","发现缓存");
            processData(cache);
        }else {
            //请求服务器
            getDataFromServer();
        }
    }

    //从服务器获取数据
    private void getDataFromServer() {
        Log.d("TAG","开始获取服务器数据");
        RequestParams params = new RequestParams(Constant.SERVER_CATEGORY_URL);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.d("NewsCenterPage",result);
                //Gson解析json数据
                processData(result);
                CacheUtil.setCache(mActivity,Constant.SERVER_CATEGORY_URL,result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d("TAG","onError");
                Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_SHORT).show();
            }


            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void processData(String result) {
        Gson gson = new Gson();
        newsCategory = gson.fromJson(result, NewsCategory.class);
        Log.d("TAG","解析："+ newsCategory);

        MainActivity activity = (MainActivity) mActivity;
        LeftMenuFragment leftMenuFragment = activity.getLeftMenuFragment();
        leftMenuFragment.setMenuData(newsCategory.data);

        left_menu_pages = new ArrayList<>();
        left_menu_pages.add(new NewsMenuDetailsPager(mActivity));
        left_menu_pages.add(new TopicMenuDetailsPager(mActivity));
        left_menu_pages.add(new PictureMenuDetailsPager(mActivity));
        left_menu_pages.add(new InteractMenuDetailsPager(mActivity));

        //将新闻菜单详情页设置为默认页面
        setCurrentDetailPager(0);
    }

    public void setCurrentDetailPager(int position) {
        BaseMenuDetailPage page = left_menu_pages.get(position);
        View view = page.mRootView;

        fl_page_content.removeAllViews();
        fl_page_content.addView(view);
        page.initData();

        tv_page_title.setText(newsCategory.data.get(position).title);
    }
}
